package com.kxs.stu_manager.global;

import java.sql.*;

/**
 * @ClassName JdbcUtils
 * @Description: TODO JDBC的工具类
 * @Author: 2119475835@qq.com
 * @Date: 2023/1/9
 */
public class JdbcUtils {
    private static String URL = "jdbc:mysql://localhost:3306/kxs_data";
    private static String USER = "root";
    private static String PASSWORD = "toor";

    // 构造方法私有化
    private JdbcUtils(){
    }

    // 注册驱动
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    // 建立连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }

    // 释放资源
    public static void free(ResultSet rs, Statement st, Connection conn){
        try {
            if(null != rs){
                rs.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            try {
                if(null != st){
                    st.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }finally {
                try {
                    if(null != conn){
                        conn.close();
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
